﻿package com.lin.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Parcelable;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.Platform.ShareParams;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

import com.lin.views.QRCodeBitmapView;
import com.t9search.util.PinyinUtil;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.BaseActivity;
import com.yunfeng.supercall.activity.LoginActivity;
import com.yunfeng.supercall.bean.PhoneContactsBean;
import com.yunfeng.supercall.manager.ShareManager;

public class Utils {

	public static Uri getURI(Context context, int resID) {
		Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
				+ context.getResources().getResourcePackageName(resID) + "/"
				+ context.getResources().getResourceTypeName(resID) + "/"
				+ context.getResources().getResourceEntryName(resID));
		return uri;
	}

	public static void createShortCut(Activity act, int iconResId,
			int appnameResId) {

		// com.android.launcher.permission.INSTALL_SHORTCUT

		Intent shortcutintent = new Intent(
				"com.android.launcher.action.INSTALL_SHORTCUT");
		// 不允许重复创建
		shortcutintent.putExtra("duplicate", false);
		// 需要现实的名称
		shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
				act.getString(appnameResId));
		// 快捷图片
		Parcelable icon = Intent.ShortcutIconResource.fromContext(
				act.getApplicationContext(), iconResId);
		shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
		// 点击快捷图片，运行的程序主入口
		shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
				new Intent(act.getApplicationContext(),
						com.yunfeng.supercall.activity.LoginActivity.class));

		// 发送广播
		act.sendBroadcast(shortcutintent);
	}

	public static void addShortcut(Activity act, String name) {
		Intent addShortcutIntent = new Intent(
				"com.android.launcher.action.INSTALL_SHORTCUT");

		// 不允许重复创建
		addShortcutIntent.putExtra("duplicate", false);// 经测试不是根据快捷方式的名字判断重复的
		// 应该是根据快链的Intent来判断是否重复的,即Intent.EXTRA_SHORTCUT_INTENT字段的value
		// 但是名称不同时，虽然有的手机系统会显示Toast提示重复，仍然会建立快链
		// 屏幕上没有空间时会提示
		// 注意：重复创建的行为MIUI和三星手机上不太一样，小米上似乎不能重复创建快捷方式

		// 名字
		addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);

		// 图标
		addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
				Intent.ShortcutIconResource.fromContext(act,
						R.drawable.ic_launcher));

		// 设置关联程序
		Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
		launcherIntent.setClass(act,
				com.yunfeng.supercall.activity.WelcomeActivity.class);
		launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);

		addShortcutIntent
				.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);

		// 发送广播
		act.sendBroadcast(addShortcutIntent);
	}

	public static void addShortCut(Activity actitivy) {

		Intent shortcut = new Intent(
				"com.android.launcher.action.INSTALL_SHORTCUT");
		// 设置属性
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, actitivy.getResources()
				.getString(R.string.app_name));
		ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
				actitivy.getApplicationContext(), R.drawable.ic_launcher);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, iconRes);

		// 是否允许重复创建
		shortcut.putExtra("duplicate", false);

		// 设置桌面快捷方式的图标
		Parcelable icon = Intent.ShortcutIconResource.fromContext(actitivy,
				R.drawable.ic_launcher);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

		// 点击快捷方式的操作
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
		intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		intent.setClass(actitivy, LoginActivity.class);

		// 设置启动程序
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);

		// 广播通知桌面去创建
		actitivy.sendBroadcast(shortcut);
	}

	public static boolean isMiu() {
		String model = Build.MODEL;
		if (model.contains("MI")) {
			return true;
		} else {
			return false;
		}
	}
	
	//=======================================================
	
	public static void ring(Context context, int rawId) {
		MediaPlayer mp = MediaPlayer.create(context, rawId);
		mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
			@Override
			public void onCompletion(MediaPlayer arg0) {
				try {
					arg0.release();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		mp.setOnErrorListener(new MediaPlayer.OnErrorListener() {
			@Override
			public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
				try {
					arg0.release();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return false;
			}
		});
		try {
			if (mp != null) {
				mp.stop();
			}
			mp.prepare();
			mp.start();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void setUpImage(final Context context, View v) {
		if (v instanceof ImageView) {
			v.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					int img = v.getId();
					String platform = null;
					switch (v.getId()) {
					case R.id.wechat_friends:
						platform = Wechat.NAME;
						break;
					case R.id.wechat_circle:
						platform = WechatMoments.NAME;
						break;
					case R.id.weibo:
						ShareManager.shareToWeiBo(context);
						break;
					case R.id.qq:
						platform = QQ.NAME;
						break;
					case R.id.sms_share:
						sendSMS(context);
						break;
					}
					try {
						if (platform != null) {
							share(context, new ShareListener(context), platform);
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
		} else if (v instanceof ViewGroup) {
			for (int i = 0; i < ((ViewGroup) v).getChildCount(); i++) {
				setUpImage(context, ((ViewGroup) v).getChildAt(i));
			}
		}
	}

	public static void share(Context context, PlatformActionListener listener, String platform) {
		int userid = BaseActivity.getUser(context).getUserid();
		if (platform == null) {
			return;
		}
		ShareParams sp = new ShareParams();
		sp.setShareType(Platform.SHARE_TEXT);
		sp.setShareType(Platform.SHARE_WEBPAGE);
		sp.setTitle(context.getResources().getString(R.string.title_share));
		sp.setText(context.getResources().getString(R.string.sms_share, userid));
		sp.setUrl(context.getResources().getString(R.string.url, userid));
		sp.setImageData(QRCodeBitmapView.createBitmap(context));

		Platform plat = ShareSDK.getPlatform(context, platform);
		plat.setPlatformActionListener(listener);
		plat.share(sp);
	}
	
	public static void sendSMS(Context context) {
		String content = context.getResources().getString(R.string.sms_share);
		Uri smsToUri = Uri.parse("smsto:");
		Intent intent = new Intent(android.content.Intent.ACTION_SENDTO, smsToUri);
		intent.putExtra("sms_body", content);
		context.startActivity(intent);
	}
	
	//=========================================================================

	public static void sendSMS(Activity mContext, String content) {
		Uri smsToUri = Uri.parse("smsto:");
		Intent mIntent = new Intent(android.content.Intent.ACTION_SENDTO,
				smsToUri);
		mIntent.putExtra("sms_body", content);
		mContext.startActivity(mIntent);
	}

	public static void sendSMS(Activity mContext, String content,
			String phoneNumber) {
		Uri smsToUri = Uri.parse("smsto:" + phoneNumber);
		Intent mIntent = new Intent(android.content.Intent.ACTION_SENDTO,
				smsToUri);
		mIntent.putExtra("sms_body", content);
		mContext.startActivity(mIntent);
	}

	/**
	 * 调起系统发短信功能
	 * 
	 * @param phoneNumber
	 *            发送短信的接收号码
	 * @param message
	 *            短信内容
	 */
	public static void SendSMSTo(Activity mContext, String phoneNumber) {
		Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"
				+ phoneNumber));
		intent.putExtra("sms_body", "");
		mContext.startActivity(intent);
	}

	/*public static void showShare(Activity mActivity) {
		ShareSDK.initSDK(mActivity);
		OnekeyShare oks = new OnekeyShare();
		// 关闭sso授权
		oks.disableSSOWhenAuthorize();

		// 分享时Notification的图标和文字 2.5.9以后的版本不调用此方法
		// oks.setNotification(R.drawable.ic_launcher,
		// getString(R.string.app_name));
		// title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
		oks.setTitle("share");
		// titleUrl是标题的网络链接，仅在人人网和QQ空间使用
		oks.setTitleUrl("http://sharesdk.cn");
		// text是分享文本，所有平台都需要这个字段
		oks.setText("我是分享文本");
		// imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
		oks.setImagePath("/sdcard/test.jpg");// 确保SDcard下面存在此张图片
		// url仅在微信（包括好友和朋友圈）中使用
		oks.setUrl("http://sharesdk.cn");
		// comment是我对这条分享的评论，仅在人人网和QQ空间使用
		oks.setComment("我是测试评论文本");
		// site是分享此内容的网站名称，仅在QQ空间使用
		oks.setSite("site");
		// siteUrl是分享此内容的网站地址，仅在QQ空间使用
		oks.setSiteUrl("http://sharesdk.cn");

		// 启动分享GUI
		oks.show(mActivity);
	}*/

	/*public static void shareWechatCircle(Activity mActivity,
			SuperFriendDynamicFragment mFragment, int userid) {
		ShareParams shareParams;
		ShareParams sp = new ShareParams();
		sp.setShareType(Platform.SHARE_TEXT);
		sp.setShareType(Platform.SHARE_WEBPAGE);

		sp.setTitle(mActivity.getResources().getString(R.string.title_share));
		sp.setText(mActivity.getResources().getString(R.string.sms_share,
				userid));
		sp.setUrl(mActivity.getResources().getString(R.string.url, userid));
		// sp.setImageUrl("http://h.hiphotos.baidu.com/image/pic/item/ac4bd11373f082028dc9b2a749fbfbedaa641bca.jpg");
		sp.setImageData(BitmapFactory.decodeResource(mActivity.getResources(),
				R.drawable.big_image));
		shareParams = sp;

		Platform plat = null;
		plat = ShareSDK.getPlatform(mActivity, "WechatMoments");
		plat.setPlatformActionListener(mFragment);

		plat.share(shareParams);
	}

	public static void shareWechatCircle(Activity mActivity,
			MeFragment mFragment, int userid) {
		ShareParams shareParams;
		ShareParams sp = new ShareParams();
		sp.setShareType(Platform.SHARE_TEXT);
		sp.setShareType(Platform.SHARE_WEBPAGE);

		sp.setTitle(mActivity.getResources().getString(R.string.title_share));
		sp.setText(mActivity.getResources().getString(R.string.sms_share,
				userid));
		sp.setUrl(mActivity.getResources().getString(R.string.url, userid));
		// sp.setImageUrl("http://h.hiphotos.baidu.com/image/pic/item/ac4bd11373f082028dc9b2a749fbfbedaa641bca.jpg");
		sp.setImageData(BitmapFactory.decodeResource(mActivity.getResources(),
				R.drawable.big_image));
		shareParams = sp;

		Platform plat = null;
		plat = ShareSDK.getPlatform(mActivity, "WechatMoments");
		plat.setPlatformActionListener(mFragment);

		plat.share(shareParams);
	}*/

	/*public static void shareWechatCircle(Activity mActivity,
			MineFragment mFragment, int userid) {
		ShareParams shareParams;
		ShareParams sp = new ShareParams();
		sp.setShareType(Platform.SHARE_TEXT);
		sp.setShareType(Platform.SHARE_WEBPAGE);

		sp.setTitle(mActivity.getResources().getString(R.string.title_share));
		sp.setText(mActivity.getResources().getString(R.string.sms_share,
				userid));
		sp.setUrl(mActivity.getResources().getString(R.string.url, userid));
		// sp.setImageUrl("http://h.hiphotos.baidu.com/image/pic/item/ac4bd11373f082028dc9b2a749fbfbedaa641bca.jpg");
		sp.setImageData(BitmapFactory.decodeResource(mActivity.getResources(),
				R.drawable.big_image));
		shareParams = sp;

		Platform plat = null;
		plat = ShareSDK.getPlatform(mActivity, "WechatMoments");
		plat.setPlatformActionListener(mFragment);

		plat.share(shareParams);
	}*/

	/*
	 * public static void shareWechatCircle(Activity mActivity, MeFragment
	 * mFragment) { ShareParams sp = new ShareParams(); sp.setTitle("title"); //
	 * sp.setTitleUrl("url"); // 标题的超链接 // sp.setText("text"); sp.setImageUrl(
	 * "http://h.hiphotos.baidu.com/image/pic/item/ac4bd11373f082028dc9b2a749fbfbedaa641bca.jpg"
	 * ); sp.setComment("comment"); sp.setSite("site");
	 * sp.setSiteUrl("siteUrl");
	 * 
	 * Platform qzone = ShareSDK.getPlatform(mActivity.getApplicationContext(),
	 * "WechatMoments");
	 * 
	 * qzone.setPlatformActionListener(mFragment); // 设置分享事件回调 // // 执行图文分享
	 * qzone.share(sp); }
	 */

	/*public static void shareWechatToFriends(Activity mActivity,
			SuperFriendDynamicFragment mFragment, int userid) {
		ShareParams shareParams;
		ShareParams sp = new ShareParams();
		// sp.setShareType(Platform.SHARE_TEXT);
		sp.setShareType(Platform.SHARE_WEBPAGE);
		sp.setShareType(Platform.SHARE_IMAGE);
		// sp.setShareType(Platform.SHARE_WEBPAGE);
		sp.setContentType(Platform.SHARE_WEBPAGE);
		sp.setContentType(Platform.SHARE_IMAGE);
		sp.setTitle(mActivity.getResources().getString(R.string.title_share));

		sp.setText(mActivity.getResources().getString(R.string.sms_share,
				userid));
		sp.setSiteUrl(mActivity.getResources().getString(R.string.url, userid));
		sp.setUrl(mActivity.getResources().getString(R.string.url, userid));

		// sp.setImageData(BitmapFactory.decodeResource(mActivity.getResources(),
		// R.drawable.ic_launcher));
		// sp.setImageUrl(mActivity.getResources().getString(R.string.url,
		// userid));
		shareParams = sp;

		Platform plat = null;
		plat = ShareSDK.getPlatform(mActivity, "Wechat");
		plat.setPlatformActionListener(mFragment);

		plat.share(shareParams);
	}*/

	/*public static void shareWechatToFriends(Activity mActivity,
			MeFragment mFragment, int userid) {
		ShareParams shareParams;
		ShareParams sp = new ShareParams();
		// sp.setShareType(Platform.SHARE_TEXT);
		sp.setShareType(Platform.SHARE_WEBPAGE);
		sp.setShareType(Platform.SHARE_IMAGE);
		// sp.setShareType(Platform.SHARE_WEBPAGE);
		sp.setContentType(Platform.SHARE_WEBPAGE);
		sp.setContentType(Platform.SHARE_IMAGE);
		sp.setTitle(mActivity.getResources().getString(R.string.title_share));

		sp.setText(mActivity.getResources().getString(R.string.sms_share,
				userid));
		sp.setSiteUrl(mActivity.getResources().getString(R.string.url, userid));
		sp.setUrl(mActivity.getResources().getString(R.string.url, userid));

		// sp.setImageData(BitmapFactory.decodeResource(mActivity.getResources(),
		// R.drawable.ic_launcher));
		// sp.setImageUrl(mActivity.getResources().getString(R.string.url,
		// userid));
		shareParams = sp;

		Platform plat = null;
		plat = ShareSDK.getPlatform(mActivity, "Wechat");
		plat.setPlatformActionListener(mFragment);

		plat.share(shareParams);
	}*/

	/*public static void shareWechatToFriends(Activity mActivity,
			MineFragment mFragment, int userid) {
		ShareParams shareParams;
		ShareParams sp = new ShareParams();
		// sp.setShareType(Platform.SHARE_TEXT);
		sp.setShareType(Platform.SHARE_WEBPAGE);
		sp.setShareType(Platform.SHARE_IMAGE);
		// sp.setShareType(Platform.SHARE_WEBPAGE);
		sp.setContentType(Platform.SHARE_WEBPAGE);
		sp.setContentType(Platform.SHARE_IMAGE);
		sp.setTitle(mActivity.getResources().getString(R.string.title_share));

		sp.setText(mActivity.getResources().getString(R.string.sms_share,
				userid));
		sp.setSiteUrl(mActivity.getResources().getString(R.string.url, userid));
		sp.setUrl(mActivity.getResources().getString(R.string.url, userid));

		// sp.setImageData(BitmapFactory.decodeResource(mActivity.getResources(),
		// R.drawable.ic_launcher));
		// sp.setImageUrl(mActivity.getResources().getString(R.string.url,
		// userid));
		shareParams = sp;

		Platform plat = null;
		plat = ShareSDK.getPlatform(mActivity, "Wechat");
		plat.setPlatformActionListener(mFragment);

		plat.share(shareParams);
	}*/

	/*public static void shareQQZone(Activity mActivity, MeFragment mFragment) {
		ShareParams sp = new ShareParams();
		sp.setTitle("title");
		sp.setTitleUrl("url"); // 标题的超链接
		sp.setText("text");
		sp.setImageUrl("imgurl");
		sp.setComment("comment");
		sp.setSite("site");
		sp.setSiteUrl("siteUrl");

		Platform qzone = ShareSDK.getPlatform(mActivity, "QZone");

		qzone.setPlatformActionListener(mFragment); // 设置分享事件回调 //
		// 执行图文分享
		qzone.share(sp);
	}*/

	public static void dial(String phone_number, Context context) {

		if (phone_number != null && !phone_number.equals("")) {

			// 调用系统的拨号服务实现电话拨打功能
			// 封装一个拨打电话的intent，并且将电话号码包装成一个Uri对象传入
			// Intent intent = new
			// Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+phone_number));
			Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
					+ phone_number));
			context.startActivity(intent);// 内部类
		}
	}

	public static void createUser(Context context) {
		Intent intent = new Intent(Intent.ACTION_INSERT);
		intent.setType("vnd.android.cursor.dir/person");
		intent.setType("vnd.android.cursor.dir/contact");
		intent.setType("vnd.android.cursor.dir/raw_contact");
		context.startActivity(intent);
	}

	public static void addContact(Context context, String contact,
			String phone, String email, String company) {
		Intent intent = new Intent(Contacts.Intents.Insert.ACTION);
		intent.setType(Contacts.People.CONTENT_TYPE);
		intent.putExtra(Contacts.Intents.Insert.NAME, contact);
		intent.putExtra(Contacts.Intents.Insert.PHONE, phone);
		intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE,
				Contacts.PhonesColumns.TYPE_MOBILE);
		intent.putExtra(Contacts.Intents.Insert.EMAIL, email);
		intent.putExtra(Contacts.Intents.Insert.COMPANY, company);
		context.startActivity(intent);
	}

	public static String getSDPath() {
		File sdDir = null;
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);
		if (sdCardExist) {
			sdDir = Environment.getExternalStorageDirectory();
		} else {
			sdDir = new File("/");
		}
		return sdDir.toString();
	}

	/**
	 * 保存文件
	 * 
	 * @param imgBitmap
	 * @param fileName
	 * @throws IOException
	 */
	public static void saveFile(URL mUrl, String fileName, Context _context) {
		Bitmap imgBitmap;
		try {
			imgBitmap = BitmapFactory.decodeStream(mUrl.openStream());

			String path = getSDPath() + "/kapai/pictures/";
			File dirFile = new File(path);
			if (!dirFile.exists()) {
				dirFile.mkdirs();
			}
			File myCaptureFile = new File(path + fileName);
			BufferedOutputStream bos;
			bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));

			imgBitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
			bos.flush();
			bos.close();
			Media.insertImage(_context.getContentResolver(), imgBitmap,
					"thisistitle", "thisisdescription").toString();
			_context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
					.parse("file://"
							+ Environment.getExternalStorageDirectory())));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@SuppressLint("NewApi")
	public static Drawable getRandomUserDrawable(Context context) {
		int size = 5;
		Random mRandom = new Random(size);
		int random = mRandom.nextInt();
		if (random <= 1) {
			return context.getDrawable(R.drawable.ic_green_user_icon);
		} else if (random <= 2) {
			return context.getDrawable(R.drawable.ic_blue_user_icon);
		} else if (random <= 3) {
			return context.getDrawable(R.drawable.ic_pink_user_icon);
		} else if (random <= 4) {
			return context.getDrawable(R.drawable.ic_purple_user_icon);
		} else {
			return context.getDrawable(R.drawable.ic_yellow_user_icon);
		}
	}

	public static ArrayList<PhoneContactsBean> getPeopleInPhone2(
			Context mContext) {
		ArrayList<PhoneContactsBean> list = new ArrayList<PhoneContactsBean>();

		Cursor cursor = mContext.getContentResolver().query(
				ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
				null, null); // 获取手机联系人
		int cpt = 0;

		int count = cursor.getCount();
		while (cursor.moveToNext()) {
			int indexPeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME); // people
			// name
			int indexPhoneNum = cursor.getColumnIndex(Phone.NUMBER); // phone
			// number
			int indexContactID = cursor.getColumnIndex(Phone.CONTACT_ID); // phone
			// number

			String strPeopleName = cursor.getString(indexPeopleName);
			String strPhoneNum = cursor.getString(indexPhoneNum);
			String strContactID = cursor.getString(indexContactID);

			/*
			 * PhoneContactsBean mPhoneContactsBean =
			 * mDatabaseHelperForChattingMap.getPhoneContactsBean(strContactID);
			 * if(mPhoneContactsBean !=null) { list.add(mPhoneContactsBean);
			 * continue; }
			 */

			PhoneContactsBean map = new PhoneContactsBean();

			map.setContactID(strContactID);
			map.setNick_name(strPeopleName);
			PinyinUtil.chineseStringToPinyinUnit(map.getNick_name(),
					map.getNamePinyinUnits());

			map.setPhone(strPhoneNum);
			map.setContactsFromPhone(true);
			list.add(map);

		}
		if (!cursor.isClosed()) {
			cursor.close();
			cursor = null;
		}

		cpt++;
		// Collections.sort(list);

		DatabaseHelperForChattingMap mDatabaseHelperForChattingMap2 = new DatabaseHelperForChattingMap(
				mContext, "db", 2);
//		mDatabaseHelperForChattingMap2.get
		
		DatabaseHelperForChattingMap mDatabaseHelperForChattingMap = new DatabaseHelperForChattingMap(
				mContext, "db", 2);
		mDatabaseHelperForChattingMap.insertPhoneContactsBeanCacheList(list);

		if (mDatabaseHelperForChattingMap != null) {
			mDatabaseHelperForChattingMap.close();
			mDatabaseHelperForChattingMap = null;
		}
		// mPhoneContacts = list;
		return list;
	}

	public static void hidenKeyBoard(Context mContext, EditText mEditText) {
		// InputMethodManager imm = (InputMethodManager) mContext
		// .getSystemService(Context.INPUT_METHOD_SERVICE);
		// imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
		InputMethodManager imm = (InputMethodManager) mContext
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
	}

	public static void toSMSScreen(Activity mActivity) {

		Intent intent4 = new Intent();
		intent4.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent4.addCategory(Intent.CATEGORY_LAUNCHER);
		// intent4.setClassName("com.android.mms","com.android.mms.ui.ConversationList");
		intent4.setClassName("com.android.mms",
				"com.android.mms.ui.ConversationList");
		mActivity.startActivity(intent4);
	}

	public static void toSMSScreen2(Activity mActivity) {

		Intent intent4 = new Intent();
		intent4.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent4.addCategory(Intent.CATEGORY_LAUNCHER);
		// intent4.setClassName("com.android.mms","com.android.mms.ui.ConversationList");
		// intent4.setClassName("com.android.mms","com.android.mms.ui.ConversationList");
		intent4.setClassName("com.android.mms",
				"com.android.mms.ui.ConversationComposer");
		mActivity.startActivity(intent4);
	}
}
